import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTabbedPane;
import javax.swing.JTextArea;


@SuppressWarnings("serial")
public class Interface extends JFrame implements ActionListener
{
	/* 
	 * Class constructing the interface
	 */
	
	private Set s;
	
	private JPanel panel;
	private JPanel panel_west;
	
	private JButton button_CC;
	private JButton button_CCS;
	private JButton button_CCM;
	private JButton button_CCMS;
	private JButton button_generate;

	private JTabbedPane onglets = null;
	
	private JTextArea text1 = new JTextArea();
	private JTextArea text2 = new JTextArea();
	private JTextArea text3 = new JTextArea();
	private JTextArea text4 = new JTextArea();
	private JTextArea text5 = new JTextArea();
	private JTextArea text6 = new JTextArea();
	
	private JLabel nb1 = new JLabel ();
	private JLabel nb2 = new JLabel ();
	private JLabel nb3 = new JLabel ();
	private JLabel nb4 = new JLabel ();
	private JLabel nb5 = new JLabel ();
	private JLabel nb6 = new JLabel ();
	
	private JMenuItem menuItem1;
	private JMenuItem menuItem2;
	
	// Constructor
	public Interface ()
	{
		s = new Set();
		s.construct_X("ACGT", 6, s.set5, s.set6, s.set6A, s.set6M, s.set6AM);
		
		// Windows settings
		this.setTitle("Dinucleotids circular codes");
		this.setSize(650, 280);
		//this.setResizable(false);
		this.setLocationRelativeTo(null);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Panel
		panel = new JPanel();
		panel_west = new JPanel(new GridLayout(6, 1));
		
		panel.setLayout(new BorderLayout());
		panel.add(panel_west, BorderLayout.WEST);
		
		// Buttons
		button_CC = new JButton("CC");
		button_CC.addActionListener(this);
		panel_west.add(button_CC);
		
		button_CCS = new JButton("CCS");
		button_CCS.addActionListener(this);
		panel_west.add(button_CCS);
		
		button_CCM = new JButton("CCM");
		button_CCM.addActionListener(this);
		panel_west.add(button_CCM);
		
		button_CCMS = new JButton("CCMS");
		button_CCMS.addActionListener(this);
		panel_west.add(button_CCMS);
		
		panel_west.add(new JLabel (
			new ImageIcon (ClassLoader.getSystemClassLoader().getResource("adn.png"))));
		
		button_generate = new JButton("Generate files");
		button_generate.addActionListener(this);
		panel_west.add(button_generate);
				
		onglets = new JTabbedPane();
        onglets.addTab("Length 1", null, getOnglet1(), null); 
        onglets.addTab("Length 2", null, getOnglet2(), null); 
        onglets.addTab("Length 3", null, getOnglet3(), null); 
        onglets.addTab("Length 4", null, getOnglet4(), null); 
        onglets.addTab("Length 5", null, getOnglet5(), null); 
        onglets.addTab("Length 6", null, getOnglet6(), null); 
              
        panel.add(onglets);
       
      //Where the GUI is created:
        JMenuBar menuBar;
        JMenu menu1, menu2;

        //Create the menu bar.
        menuBar = new JMenuBar();

        //Build the first menu.
        menu1 = new JMenu("File");
        menu2 = new JMenu("About");
        menuBar.add(menu1);
        menuBar.add(menu2);

        //a group of JMenuItems
        menuItem1 = new JMenuItem("Quit");
        menuItem1.addActionListener(this);
        menuItem2 = new JMenuItem("Help");
        menuItem2.addActionListener(this);
 
        menu1.add(menuItem1);
        menu2.add(menuItem2);
        
        this.setJMenuBar(menuBar);     
        this.setIconImage(new ImageIcon(ClassLoader.getSystemClassLoader().getResource("adn-mini.png")).getImage());
		this.setContentPane(panel);
		this.setVisible(true);	
	}

	public JPanel getOnglet1()
	{
		JPanel p = new JPanel();
		JPanel p1 = new JPanel();
		
		JLabel j1 = new JLabel("Number of codes: ");
		nb1.setText("" + s.set1.size());
	
		p1.add(j1);
		p1.add(nb1);
		
		JScrollPane scroll_text = new JScrollPane(text1);
				
		// TextArea
		text1.setEditable(false);
		text1.setText(s.display(s.set1));
		
		p.setLayout(new BorderLayout());
		p.add(p1, BorderLayout.NORTH);
		
		p.add(scroll_text);
				
		return p;
	}
	
	public JPanel getOnglet2()
	{
		JPanel p = new JPanel();
		JPanel p1 = new JPanel();
		
		JLabel j1 = new JLabel("Number of codes: ");
		nb2.setText("" + s.set2.size());
	
		p1.add(j1);
		p1.add(nb2);
		
		JScrollPane scroll_text = new JScrollPane(text2);
		
		// TextArea
		text2.setEditable(false);
		text2.setText(s.display(s.set2));
				
		p.setLayout(new BorderLayout());
		p.add(p1, BorderLayout.NORTH);
		
		p.add(scroll_text);
				
		return p;
	}
	
	public JPanel getOnglet3()
	{
		JPanel p = new JPanel();
		JPanel p1 = new JPanel();
		
		JLabel j1 = new JLabel("Number of codes: ");
		nb3.setText("" + s.set3.size());
	
		p1.add(j1);
		p1.add(nb3);
		
		JScrollPane scroll_text = new JScrollPane(text3);
		
		// TextArea
		text3.setEditable(false);
		text3.setText(s.display(s.set3));
				
		p.setLayout(new BorderLayout());
		p.add(p1, BorderLayout.NORTH);
		
		p.add(scroll_text);
				
		return p;
	}
	
	public JPanel getOnglet4()
	{
		JPanel p = new JPanel();
		JPanel p1 = new JPanel();
		
		JLabel j1 = new JLabel("Number of codes: ");
		nb4.setText("" + s.set4.size());
	
		p1.add(j1);
		p1.add(nb4);
		
		JScrollPane scroll_text = new JScrollPane(text4);
				
		// TextArea
		text4.setEditable(false);
		text4.setText(s.display(s.set4));
		
		p.setLayout(new BorderLayout());
		p.add(p1, BorderLayout.NORTH);
		
		p.add(scroll_text);
				
		return p;
	}
	
	public JPanel getOnglet5()
	{
		JPanel p = new JPanel();
		JPanel p1 = new JPanel();
		
		JLabel j1 = new JLabel("Number of codes: ");
		nb5.setText("" + s.set5.size());
	
		p1.add(j1);
		p1.add(nb5);
		
		JScrollPane scroll_text = new JScrollPane(text5);
				
		// TextArea
		text5.setEditable(false);
		text5.setText(s.display(s.set5));
		
		p.setLayout(new BorderLayout());
		p.add(p1, BorderLayout.NORTH);
		
		p.add(scroll_text);
				
		return p;
	}
	
	public JPanel getOnglet6()
	{
		JPanel p = new JPanel();
		JPanel p1 = new JPanel();
		
		JLabel j1 = new JLabel("Number of codes: ");
		nb6.setText("" + s.set6.size());
	
		p1.add(j1);
		p1.add(nb6);
		
		JScrollPane scroll_text = new JScrollPane(text6);
		
		// TextArea
		text6.setEditable(false);
		text6.setText(s.display(s.set6));
				
		p.setLayout(new BorderLayout());
		p.add(p1, BorderLayout.NORTH);
		
		p.add(scroll_text);
				
		return p;
	}
	
	public void actionPerformed(ActionEvent e) 
	{
		if (e.getSource() == button_CC)
		{
			nb1.setText("" + s.set1.size());
			text1.setText(s.display(s.set1));
			
			nb2.setText("" + s.set2.size());
			text2.setText(s.display(s.set2));
			
			nb3.setText("" + s.set3.size());
			text3.setText(s.display(s.set3));
			
			nb4.setText("" + s.set4.size());
			text4.setText(s.display(s.set4));
			
			nb5.setText("" + s.set5.size());
			text5.setText(s.display(s.set5));
			
			nb6.setText("" + s.set6.size());
			text6.setText(s.display(s.set6));
		}
		else
		if (e.getSource() == button_CCS)
		{
			nb1.setText("" + s.set1A.size());
			text1.setText(s.display(s.set1A));
			
			nb2.setText("" + s.set2A.size());
			text2.setText(s.display(s.set2A));
			
			nb3.setText("" + s.set3A.size());
			text3.setText(s.display(s.set3A));
			
			nb4.setText("" + s.set4A.size());
			text4.setText(s.display(s.set4A));
			
			nb5.setText("" + s.set5A.size());
			text5.setText(s.display(s.set5A));
			
			nb6.setText("" + s.set6A.size());
			text6.setText(s.display(s.set6A));
		}	
		else
		if (e.getSource() == button_CCM)
		{
			nb1.setText("" + s.set1M.size());
			text1.setText(s.display(s.set1M));
			
			nb2.setText("" + s.set2M.size());
			text2.setText(s.display(s.set2M));
			
			nb3.setText("" + s.set3M.size());
			text3.setText(s.display(s.set3M));
			
			nb4.setText("" + s.set4M.size());
			text4.setText(s.display(s.set4M));
			
			nb5.setText("" + s.set5M.size());
			text5.setText(s.display(s.set5M));
			
			nb6.setText("" + s.set6M.size());
			text6.setText(s.display(s.set6M));
		}
		else
		if (e.getSource() == button_CCMS)
		{
			nb1.setText("" + s.set1AM.size());
			text1.setText(s.display(s.set1AM));
			
			nb2.setText("" + s.set2AM.size());
			text2.setText(s.display(s.set2AM));
			
			nb3.setText("" + s.set3AM.size());
			text3.setText(s.display(s.set3AM));
			
			nb4.setText("" + s.set4AM.size());
			text4.setText(s.display(s.set4AM));
			
			nb5.setText("" + s.set5AM.size());
			text5.setText(s.display(s.set5AM));
			
			nb6.setText("" + s.set6AM.size());
			text6.setText(s.display(s.set6AM));
		}
		else if (e.getSource() == button_generate)
		{
			s.generate();
			
			// Display popup
			JOptionPane.showMessageDialog(null,
				"Codes have been generated, you can check \nthem through the interface or via text files.",
				"Codes generated",
				JOptionPane.INFORMATION_MESSAGE,
				null);
		}
		else if (e.getSource() == menuItem1)
		{
			this.dispose();
		}
		else if (e.getSource() == menuItem2)
		{
			// Display authors
			JOptionPane.showMessageDialog(null,
				"This software has been developped by S.Wilt and L. Specht.\nCopyright 2012",
				"About",
				JOptionPane.INFORMATION_MESSAGE,
				null);
		}
	}
}
